﻿using System;

public partial class aspx_admin_video_insert : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txt发布时间.Text = DateTime.Now.ToString();
            txt点击量.Text = "100";
        }
    }

    protected void ddl视频类别_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.mv视频类别.ActiveViewIndex = this.ddl视频类别.SelectedIndex;
    }

    protected void InsertButton_Click(object sender, EventArgs e)
    {
        DateTime dt = DateTime.Now;
        int dianji = 0;
        if (!DateTime.TryParse(txt发布时间.Text, out dt))
        {
            WebHelper.MessageBox.Show("请输入正确的时间。");
            return;
        }
        if (!int.TryParse(txt点击量.Text, out dianji))
        {
            WebHelper.MessageBox.Show("输入的点击量不正确。");
            return;
        }
        int id = int.Parse(ddl讲堂ID.SelectedValue);

        switch (ddl视频类别.SelectedIndex)
        {
            case 0:

                using (ds_名师讲堂TableAdapters.tb_讲堂视频TableAdapter da = new ds_名师讲堂TableAdapters.tb_讲堂视频TableAdapter())
                {
                    if (da.GetDataBy讲堂ID(id).Rows.Count > 0)
                    {
                        WebHelper.MessageBox.Show("已有此讲座的视频！", true);
                        return;
                    }
                }

                break;

            case 1:
                break;

            default:
                break;
        }
        string filename = string.Empty;
        if (fu.HasFile)
        {
            filename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(fu.FileName);
            try
            {
                WebHelper.WebControls.UploadFile(
                         fu,
                         "~/video/",
                         new string[] { ".flv" },
                         filename,
                         false,
                         int.MaxValue);
            }
            catch (Exception)
            {
                throw;
            }
        }
        else
        {
            WebHelper.MessageBox.Show("新选择需要上传的视频。(.flv格式)");
            return;
        }
        switch (ddl视频类别.SelectedIndex)
        {
            case 0:
                string title = string.Empty;
                using (ds_名师讲堂TableAdapters.tb_讲堂TableAdapter da = new ds_名师讲堂TableAdapters.tb_讲堂TableAdapter())
                {
                    ds_名师讲堂.tb_讲堂Row dr = da.GetDataBy讲堂ID(id).Rows[0] as ds_名师讲堂.tb_讲堂Row;
                    title = dr.标题;
                }
                int idd = 0;
                using (ds_名师讲堂TableAdapters.tb_视频TableAdapter da = new ds_名师讲堂TableAdapters.tb_视频TableAdapter())
                {
                    da.Insert(title, "", filename, dianji, dt, ccb置顶.Checked);
                    idd = (da.GetDataBy模糊查询标题(title).Rows[0] as ds_名师讲堂.tb_视频Row).视频ID;
                    
                }
                using (ds_名师讲堂TableAdapters.tb_讲堂视频TableAdapter da = new ds_名师讲堂TableAdapters.tb_讲堂视频TableAdapter())
                {
                    da.Insert(id, idd);
                }
                break;

            case 1:
                using (ds_名师讲堂TableAdapters.tb_视频TableAdapter da = new ds_名师讲堂TableAdapters.tb_视频TableAdapter())
                {
                    da.Insert(txt标题.Text, txt详情.Text, filename, dianji, dt, ccb置顶.Checked);
                }
                break;

            default:
                break;
        }
        WebHelper.MessageBox.Show("添加成功！");
    }
}